% $Id: e_filt.m 320 2005-05-05 20:07:09Z odellj $
% e_filt.m subroutine version of eros_filt.m (4/18/03) - filter EROS data
% $Log: e_filt.m,v $
%
% 2011/11/14 - filter only "good" channels; write "good_* chs to output file
%
% Revision 1.8  2005/02/03 22:08:55  odellj
% If the overwrite flag is not checked, will correctly not try to refilter now.
%
% Revision 1.7  2004/08/25 22:15:06  odellj
% Added the ability to pass the id_tag into the id_tags structure if this routine is called.
%
% Revision 1.6  2004/07/13 22:22:02  odellj
% Added the new variable _id to the subprogram to output to the inidiviually created files to identify the version of the producing function.
%
% Revision 1.5  2004/07/09 21:50:42  odellj
% Updated the logging of the subroutines.  Added log_file as a parameter for each of their calls.
%
% Revision 1.4  2004/07/07 18:08:12  odellj
% Prepped for logging.
%
% Revision 1.3  2004/07/06 20:32:00  edmaclin
% Fixed merged issues
%
% Revision 1.2  2004/06/30 18:14:03  odellj
% Added more verbose fprintf statements.
%
% Revision 1.1.1.1  2004/02/18 20:09:49  odellj
% initial import into CVS
%
% Revision 1.2  2004/02/05 20:47:13  odellj
% Added Id and Log tags.
%

function e_filt(good_dist_chs,log_file)

global hdr handles file_name id_tags

fprintf('e_filt...\n');
e_filt_id = '$Id: e_filt.m 320 2005-05-05 20:07:09Z odellj $';
check = strcmp(e_filt_id,id_tags); % Make sure we don't already have the tag
if sum(check) == 0
    id_tags{size(id_tags,1)+1,1} = e_filt_id;  % Add it in then.
end

in_path=[hdr.data_path hdr.exp_name hdr.data_type '00-00\'];
out_path=[hdr.data_path hdr.exp_name hdr.data_type hdr.hi '-' hdr.lo '\'];
status=dos(['mkdir ' hdr.data_path hdr.exp_name hdr.data_type hdr.hi '-' hdr.lo]);

if ~strcmp(log_file,'')
    logid = fopen(log_file,'a');
else logid = 1; end

% Check to see if the file already exists
%
in_file_flg=fopen([in_path file_name]);
out_file_flg=fopen([out_path file_name]);

[status,msg] = mkdir(out_path); % Make the output directory if it doesn't exixt

if ((in_file_flg>0) & (out_file_flg<0))

    nf=hdr.sample_rate/2;   % Low & High pass freqs are specified as proportion of Nyquist freq
    order=12; % originally used 5, Kathy needs 12 for pulse high-pass
    
    if (hdr.hi_pass > 0) & (hdr.low_pass > 0) % Define a Butterworth bandpass filter
        disp('Designing band pass filter')
    	[b,a] = butter(order,[hdr.hi_pass/nf,hdr.low_pass/nf]);  
    end

    if (hdr.hi_pass == 0) & (hdr.low_pass > 0) % Define a Butterworth lowpass filter
        disp('Designing low pass filter')
    	[b,a] = butter(order,hdr.low_pass/nf,'low');  
    end

    if (hdr.hi_pass > 0) & (hdr.low_pass == 0) % Define a Butterworth highpass filter
        disp('Designing high pass filter')
    	[b,a] = butter(order,hdr.hi_pass/nf,'high');  
    end

    % load EROS data in .mat format
    %
    fprintf(logid,'Reading %s\n',[in_path file_name]);
    load([in_path file_name],'-mat');

    if exist('good_mrac_chs')~=1
        % apply DC & AC quality thresholds
        %
        good_mrac_chs=find(mrac>=hdr.qual.norm_mrac_thr);
        bad_mrac_chs=find(mrac<hdr.qual.norm_mrac_thr);
        good_mrdc_chs=find(mrdc>=hdr.qual.norm_mrdc_thr);
        bad_mrdc_chs=find(mrdc<hdr.qual.norm_mrdc_thr);
    end
        
    good_chs=intersect(good_mrac_chs,good_mrdc_chs);
    good_chs=intersect(good_chs,good_dist_chs);
    
    % zero the bad chans ( mostly for "old" normalized data)
    %
    temp=zeros(size(dc));
    temp(:,good_chs)=dc(:,good_chs);
    dc=temp;
    temp=zeros(size(dc));
    temp(:,good_chs)=ac(:,good_chs);
    ac=temp;
    temp=zeros(size(dc));
    temp(:,good_chs)=ph(:,good_chs);
    ph=temp;
    
    fprintf(logid,'Filtering... ((e_filt.m))\n');
    for ch=good_chs  % Filter ac, dc & phase data for each channel
        tmp(:,ch) = filtfilt(b,a,dc(:,ch));
        dc(:,ch) = tmp(:,ch);
        tmp(:,ch) = filtfilt(b,a,ac(:,ch));
        ac(:,ch) = tmp(:,ch);
        tmp(:,ch) = filtfilt(b,a,ph(:,ch));
        ph(:,ch) = tmp(:,ch);
    end % ch

    if ~exist('hdr_norm','var');hdr_norm=[];end % create null var if needed
    if ~exist('hdr_pc','var');hdr_pc=[];end % create null var if needed
    hdr_filt=hdr;   % save hdr WITH UNIQUE NAME !
    msg=sprintf('Writing %s\n',[out_path file_name]);
    fprintf(logid,'Writing %s\n',[out_path file_name]);
    save([out_path file_name],'dc','ph','ac','mrdc','mrac',...
        'good_dist_chs','good_mrac_chs','good_mrdc_chs',...
        'hdr_norm','hdr_pc','hdr_filt','id_tags','-mat')

else % if various files exist or don't...
    if (in_file_flg<0)
        msg=sprintf('%s does not exist.\n',[in_path file_name]);
        fprintf(logid,'%s does not exist.\n',[in_path file_name]);
    end
    if (out_file_flg>0)
        msg=sprintf('%s already exists!\n',[out_path file_name]);
        fprintf(logid,'%s already exists!\n',[out_path file_name]);
    end
end

if logid ~= 1, fclose(logid); end
